package com.akcome.apollo.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.akcome.apollo.common.ApolloConstants;
import com.akcome.apollo.common.AuthUtils;
import com.akcome.apollo.common.TokenHelper;
import com.akcome.apollo.domain.BmpNoticeConfig;
import com.akcome.apollo.domain.DictionaryItem;
import com.akcome.apollo.service.BmpAccountService;
import com.akcome.apollo.service.BmpNoticeConfigService;
import com.akcome.apollo.service.DictionaryItemService;

@Controller
@RequestMapping("/home")
public class HomeController {

	@Autowired
	private TokenHelper tokenHelper;
	
	@Autowired
	private BmpAccountService bmpAccountService;
	
	@Autowired
	private BmpNoticeConfigService bmpNoticeConfigService;
	
	@Autowired
	private DictionaryItemService dictionaryItemService;
	
	/**
	 * 用户面板
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/user")
	public String user(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId,  ModelMap modelMap,HttpServletResponse response){
		bmpAccountService.homeSetting(modelMap,c_accountId,response);
		bmpAccountService.homeUserSetting(modelMap);
		return "home/home";
	}


	/**
	 * 旷工管理
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/worker")
	public String worker(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
		bmpAccountService.homeSetting(modelMap,c_accountId,response);
		return "home/workers";
	}
	
	/**
	 * 用户通知
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/notice")
	public String notice(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
		bmpAccountService.homeSetting(modelMap,c_accountId,response);
		BmpNoticeConfig config = bmpNoticeConfigService.findByAccountId(c_accountId);
		modelMap.put("config", config);
		return "home/userNoticeShow";
	}
	
	/**
	 * 收益记录
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/income")
	public String income(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
		bmpAccountService.homeSetting(modelMap,c_accountId,response);
		return "home/pplns";
	}
	
	/**
	 * 设置
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/setting")
	public String setting(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
		bmpAccountService.homeSetting(modelMap,c_accountId,response);
		List<DictionaryItem> payModes = dictionaryItemService.findDictionary("PAY_MODE", null);
		List<DictionaryItem> coinTypes = dictionaryItemService.findDictionary("COIN_TYPE", null);
		modelMap.put("payModes", payModes);
		modelMap.put("coinTypes", coinTypes);
		return "home/settings";
	}
	
	/**
	 * 切换账户
	 * @param id
	 * @param modelMap
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accountChange/{id}")
	public boolean accountChange(@PathVariable("id") Long id,ModelMap modelMap,HttpServletResponse response){
		bmpAccountService.homeSetting(modelMap, id, response);
		Cookie cookie = new Cookie(ApolloConstants.COOKIE_ACCOUNT, ""+id);
		cookie.setPath("/");
		cookie.setMaxAge(tokenHelper.getExpiration());
		response.addCookie(cookie);
		return true;
	}
	
	/**
     * 修改登录密码
     * @return
     */
    @RequestMapping("/editPwd")
    public String editPwd(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
    	bmpAccountService.homeSetting(modelMap, c_accountId, response);
    	return "home/editPwd";
    }
	
    /**
     * 绑定手机
     * @param c_accountId
     * @param modelMap
     * @return
     */
    @RequestMapping("/bindMobile")
	public String bindMobile(@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
    	bmpAccountService.homeSetting(modelMap, c_accountId, response);
    	modelMap.put("userId", AuthUtils.getUserId());
    	return "home/bindMobile";
	}
}
